﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Models.Extensions
{
    /// <summary>
    /// Extend the functionality of the .NET dictionary. Useful as connection settings/plugins are stored in a dictionary
    /// </summary>
    public static class DictionaryExtension
    {
        /// <summary>
        /// Add one dictionary to another. If the dictionary being added to already contains a key that is in the dictionary
        /// that we are adding then the original value is kept and the value in the source dictionary is discarded
        /// </summary>
        /// <typeparam name="K">The Key type</typeparam>
        /// <typeparam name="V">The Value type</typeparam>
        /// <param name="item">The dictionary we are adding to</param>
        /// <param name="source">The dictionary containing the items to add</param>
        public static void AddRange<K, V>(this Dictionary<K, V> item, Dictionary<K, V> source)
        {
            foreach (KeyValuePair<K,V> soureItem in source)
            {
                // If the destination dictionary does not already contain this item then add it
                if (!item.ContainsKey(soureItem.Key))
                {
                    item.Add(soureItem.Key, soureItem.Value);
                }
            }
        }
    }
}
